<?php
/**
 * @author Gideon Amissah<kuulmek@yahoo.com>
 * @access public
 * @version 
 */

$pay_type_arr = explode(';',PAYMENT_TYPE);
$pay_type_opt = array();
foreach($pay_type_arr as $type_opt){
    $pay_type_opt[$type_opt] = $type_opt;
}

/** Get the last recent bill applied and use that for payment **/

$len = count($billData);
if($len > 0){
   $recent =  $billData[$len-1];
}

?>
<style type="text/css">    
    table{
        font-size: 12px;
        font-family: Verdana,Arial,sans-serif;
        width: 100%;    
    }
    
    .entryHeader {
    	font-family: Arial, Helvetica, sans-serif;
    	font-size: 14px;
    	color: #FFFFFF;
    	background-color: #1C1C1C;
    	font-weight: bold;
    	background-position: center;
        text-align: center;
    }
</style>
<div class="property-pane" >
   <div class="pane-header">
       Bills Payment
   </div>
<?php
if($len == 0){ /**  No bill has been applied to this property **/
    echo "Sorry this property has no Bill yet!";
}
else{
?>
    <div style="margin-top: 10px; margin-bottom: 10px;">
        <table border="0" cellpadding="0" cellspacing="5">
            <tr>
                <td valign="top">
                    <div style=" margin-right: 10px; width: 400px; float: left; border:1px solid #ddd;">
                        <table border="0" cellpadding="0" cellspacing="5" class="data_table">
                            <thead>
                                  <th colspan='2' align='center'>Property Info</th>
                            </thead>
                            <tr>
                              <td width='59' height='66px' rowspan='6' class='content'>
            	                  <?php 
                                    echo $this->Html->image('icon-14.png', array('width' => '58', 'height' => '65', 'align' => 'left', 'border' => '0'));
                                  ?>
                              </td>
                            </tr>
                            <tr>
                              <td align='left' class='content'>Propety Group: &nbsp;
                                <?php
            	                   echo $propData['PropertyGroup']['group_name']; 
                                ?>
                              </td>
                            </tr>
                            <tr>
                              <td align='left' class='content'>Location: &nbsp;
                                <?php
            	                   echo $propData['Location']['location_name']; 
                                ?>
                              </td>
                            </tr>
                            <tr>
                              <td align='left' class='content'>Property Registration No: &nbsp;
                                <?php
            	                   echo $propData['PropertyItem']['property_reg_no']; 
                                ?>
                              </td>
                            </tr>
                            <tr>
                              <td align='left' class='content'>Property Name: &nbsp;
                                <?php
            	                   echo $propData['PropertyItem']['name']; 
                                ?>
                              </td>
                            </tr>
                            <tr>
                              <td align='left' class='content'>Owner: &nbsp;
                                <?php
            	                   echo $propData['PropertyItem']['owners_name']; 
                                ?>
                              </td>
                            </tr>                
                        </table>
                    </div>
                </td>
                <td>
                     <div style="width: 450px; float: right; border: 1px solid #B2B2B2; -moz-border-radius: 5px; -webkit-border-radius: 5px; border-radius: 5px; ">
                         <table border="0" cellpadding="0" cellspacing="5" class="data_table">
                            <thead>
                                <th width="15%">Year</th>
                                <th width="20%"> <?php  echo 'Bill '.CURRENCY ;?></th>
                                <th width="25%"><?php echo 'Amount Paid '.CURRENCY ;?></th>
                                <th width="15%">Modify</th>
                            </thead>
                          </table>
                          <div  style="height: 140px; overflow: auto; margin-top: 10px;">
                              <table class="data_table" width="100%" class="scrolltable" border="0">
                                 <tbody>
                                  <?php $ttl_bill = 0.00;
                                        $ttl_payment = 0.00;
                                        foreach($billData as $bkey => $bill){
                                          $ttl_bill = $ttl_bill + $bill['Bill']['total'];
                                          $ttl_payment = $ttl_payment + $bill['Total Payment'];
                                         ?>
                                            <tr data-id="<?php echo $bill['BillProperty']['id']; ?>" property-id="<?php echo  $propData['PropertyItem']['id']; ?>">
                                                <td width="15%" align="center">
                                                    <?php echo $bill['Bill']['year']; ?>
                                                </td>
                                                <td width="20%" align="center">
                                                    <?php echo $bill['Bill']['total']; ?>
                                                </td>
                                                <td width="25%" align="center">
                                                    <?php echo $bill['Total Payment']; ?>
                                                </td>
                                                <td width="15%" align="center">
                                                    <?php echo $this->Html->link('info', 'javascript:void(0)',array('class'=>'bill_info','escape'=>false)) ;?>
                                                </td>  
                                            </tr>
                                         <?php 
                                       }
                                       $credit = 0.00;
                                       $debit = 0.00;
                                       $diff = $ttl_bill - $ttl_payment;
                                       if($diff < 0){/** Credit **/
                                            $credit = $diff * -1;
                                       }
                                       else{/** Dedit **/
                                        $debit = $diff;
                                       }
                                  ?>
                                </tbody> 
                              </table>
                          </div>
                           <table border="0" cellpadding="0" cellspacing="5" class="data_table">
                            <tr>
                                <th align="center"><?php  echo 'Total Bill: &nbsp; '.CURRENCY.' '.number_format($ttl_bill,2) ;?></th>
                            </tr>
                            <tr>
                                <th align="center"><?php echo 'Total Paid: &nbsp; '.CURRENCY.' '.number_format($ttl_payment,2) ;?></th>
                            </tr>
                            <tr>
                                <th align="center"><?php echo 'Total Credit: &nbsp; '.CURRENCY.' '.number_format($credit,2) ;?></th>
                            </tr>
                            <tr>
                                <th align="center"><?php  echo 'Total Debit: &nbsp; '.CURRENCY.' '.number_format($debit,2) ;?></th>
                            </tr>
                          </table>        
                    </div>
                </td>
            </tr>
        </table>
   </div>
   
   <div style="border: 1px solid #B2B2B2; -moz-border-radius: 5px; -webkit-border-radius: 5px; border-radius: 5px; ">
        <div class="entryHeader" style="margin-bottom: 10px;">Pay Bill</div>
        <div id="paybill-form-window" class="form-window" style="width: 600px;">
        	<div class="paybill-prompt" ></div>
            <?php echo $this->Form->create('BillPayment', array('default' => false, 'id' => 'paybill-form', 'action' => 'add','inputDefaults' => array('label' => false,'div' => false)));?>	
              <div class="content">
                   <div class="row">
                        <div class="label"><?php echo 'Amount '.CURRENCY ;?></div>
    				    <div class="data">
                            <?php echo $this->Form->input('amount', array('id'=>'amount', 'class'=>'validate[required,custom[number]]', 'div' => false, 'label' => false));?> 
                        </div>
                   </div>
                   <div class="row ">
                        <div class="label">Payment Type</div>
    				    <div class="data">
                        <?php echo $this->Form->input('type', array( 'div' => false, 'label' => false, 'id'=>'type', 'options' => $pay_type_opt));?> 
                        </div>
                   </div>
                   <div class="row ">
                        <div class="label">Received From: </div>
                        <div class="data"> 
                            <?php echo $this->Form->input('recieved_from', array('type'=>'text', 'size'=>'0', 'value'=>''.$propData['PropertyItem']['owners_name'].'', 'div' => false, 'label' => false, 'id'=>'recieved_from' ,'class' => 'validate[required]')); ?>
                        </div>
                    </div>
                    <div class="row ">
                        <div class="label">Comment</div>
    				    <div class="data">
                        <?php echo $this->Form->input('comment', array('type'=>'textarea', 'div' => false, 'label' => false, 'id'=>'comment' ,'class' => '', 'value'=>'')); ?>
                        </div>
                    </div> 
                    <div class="row">
                       <div class="label">
                         <?php echo $this->Form->input('bill_property_id', array('type'=>'hidden', 'id'=>'bill_property_id', 'value'=>''.$recent['BillProperty']['id'].'', 'div' => false, 'label' => false)); ?>
                         <?php echo $this->Form->input('bill_id', array('type'=>'hidden', 'id'=>'bill_id', 'value'=>''.$recent['BillProperty']['bill_id'].'', 'div' => false, 'label' => false)); ?>
                         <?php echo $this->Form->input('property_item_id', array('type'=>'hidden', 'id'=>'property_item_id', 'value'=>''.$recent['BillProperty']['property_item_id'].'', 'div' => false, 'label' => false)); ?>
                       </div>
                       <div class="data">
                          <button type="button" class="save" id="paybill-save-btn"><?php echo $this->Html->image('ico-49.gif', array('width' => '16', 'height' => '16', 'align' => 'left', 'border' => '0')); ?>&nbsp;Save</button>
                          <button type="button" class="cancel" id="paybill-cancel-btn"><?php echo $this->Html->image('ico-43.png', array('width' => '16', 'height' => '16', 'align' => 'left', 'border' => '0')); ?>&nbsp;Cancel</button>
                       </div>
                    </div>
                </div>
            <?php echo $this->Form->end();?>
            <div style="margin-left: 20px; margin-bottom: 20px;">
                Print Receipt After Payment &nbsp; <input  type="checkbox" id="print_receipt_chk" value="1" />
            </div>
      </div>
        
   </div>
<?php   
}
?>
   
   <div style="display: none;">
        <div id="billPaymentInfo_colorbox" style="margin: 30px 20px 20px 20px;"></div>
    </div>
    
</div>

<input type="hidden" id="pay_bill-url" value="<?php echo $this->Html->url(array('controller' => 'Account', 'action' => 'add')); ?>" />
<input type="hidden" id="print-receipt-url" value="<?php echo $this->Html->url(array('controller' => 'Account', 'action' => 'printReceipt')); ?>" />
<input type="hidden" id="bill-payment-info-url" value="<?php echo $this->Html->url(array('controller' => 'Account', 'action' => 'veiwBillPaymentInfo')); ?>" />

<?php
    echo $this->Html->script('custom-scripts/bills_payment.js',array('inline'=>false));
?>
